home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / cal-islam.el < prev    next >
Lisp/Scheme  |  1996-03-22  |  23KB  |  493 lines

  1. ;;; cal-islam.el --- calendar functions for the Islamic calendar.
  2.  
  3. ;; Copyright (C) 1995 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Edward M. Reingold <reingold@cs.uiuc.edu>
  6. ;; Keywords: calendar
  7. ;; Human-Keywords: Islamic calendar, calendar, diary
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  23. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; This collection of functions implements the features of calendar.el and
  29. ;; diary.el that deal with the Islamic calendar.
  30.  
  31. ;; Comments, corrections, and improvements should be sent to
  32. ;;  Edward M. Reingold               Department of Computer Science
  33. ;;  (217) 333-6733                   University of Illinois at Urbana-Champaign
  34. ;;  reingold@cs.uiuc.edu             1304 West Springfield Avenue
  35. ;;                                   Urbana, Illinois 61801
  36.  
  37. ;;; Code:
  38.  
  39. (require 'cal-julian)
  40.  
  41. (defvar calendar-islamic-month-name-array
  42.   ["Muharram" "Safar" "Rabi I" "Rabi II" "Jumada I" "Jumada II"
  43.    "Rajab" "Sha'ban" "Ramadan" "Shawwal" "Dhu al-Qada" "Dhu al-Hijjah"])
  44.  
  45. (defvar calendar-islamic-epoch (calendar-absolute-from-julian '(7 16 622))
  46.   "Absolute date of start of Islamic calendar = August 29, 284 A.D. (Julian).")
  47.  
  48. (defun islamic-calendar-leap-year-p (year)
  49.   "Returns t if YEAR is a leap year on the Islamic calendar."
  50.   (memq (% year 30)
  51.         (list 2 5 7 10 13 16 18 21 24 26 29)))
  52.  
  53. (defun islamic-calendar-last-day-of-month (month year)
  54.   "The last day in MONTH during YEAR on the Islamic calendar."
  55.   (cond
  56.    ((memq month (list 1 3 5 7 9 11)) 30)
  57.    ((memq month (list 2 4 6 8 10)) 29)
  58.    (t (if (islamic-calendar-leap-year-p year) 30 29))))
  59.  
  60. (defun islamic-calendar-day-number (date)
  61.   "Return the day number within the year of the Islamic date DATE."
  62.     (let* ((month (extract-calendar-month date))
  63.            (day (extract-calendar-day date)))
  64.       (+ (* 30 (/ month 2))
  65.          (* 29 (/ (1- month) 2))
  66.          day)))
  67.  
  68. (defun calendar-absolute-from-islamic (date)
  69.   "Absolute date of Islamic DATE.
  70. The absolute date is the number of days elapsed since the (imaginary)
  71. Gregorian date Sunday, December 31, 1 BC."
  72.   (let* ((month (extract-calendar-month date))
  73.          (day (extract-calendar-day date))
  74.          (year (extract-calendar-year date))
  75.          (y (% year 30))
  76.          (leap-years-in-cycle
  77.           (cond
  78.            ((< y 3) 0)  ((< y 6) 1)  ((< y 8) 2)  ((< y 11) 3) ((< y 14) 4)
  79.            ((< y 17) 5) ((< y 19) 6) ((< y 22) 7) ((< y 25) 8) ((< y 27) 9)
  80.            (t 10))))
  81.     (+ (islamic-calendar-day-number date);; days so far this year
  82.        (* (1- year) 354)                 ;; days in all non-leap years
  83.        (* 11 (/ year 30))                ;; leap days in complete cycles
  84.        leap-years-in-cycle               ;; leap days this cycle
  85.        (1- calendar-islamic-epoch))))    ;; days before start of calendar
  86.  
  87. (defun calendar-islamic-from-absolute (date)
  88.   "Compute the Islamic date (month day year) corresponding to absolute DATE.
  89. The absolute date is the number of days elapsed since the (imaginary)
  90. Gregorian date Sunday, December 31, 1 BC."
  91.   (if (< date calendar-islamic-epoch)
  92.       (list 0 0 0);; pre-Islamic date
  93.     (let* ((approx (/ (- date calendar-islamic-epoch)
  94.                       355));; Approximation from below.
  95.            (year           ;; Search forward from the approximation.
  96.             (+ approx
  97.                (calendar-sum y approx
  98.                              (>= date (calendar-absolute-from-islamic
  99.                                        (list 1 1 (1+ y))))
  100.                              1)))
  101.            (month          ;; Search forward from Muharram.
  102.             (1+ (calendar-sum m 1
  103.                               (> date
  104.                                  (calendar-absolute-from-islamic
  105.                                   (list m
  106.                                         (islamic-calendar-last-day-of-month
  107.                                          m year)
  108.                                         year)))
  109.                               1)))
  110.            (day            ;; Calculate the day by subtraction.
  111.             (- date
  112.                (1- (calendar-absolute-from-islamic (list month 1 year))))))
  113.       (list month day year))))
  114.  
  115. (defun calendar-islamic-date-string (&optional date)
  116.   "String of Islamic date before sunset of Gregorian DATE.
  117. Returns the empty string if DATE is pre-Islamic.
  118. Defaults to today's date if DATE is not given.
  119. Driven by the variable `calendar-date-display-form'."
  120.   (let ((calendar-month-name-array calendar-islamic-month-name-array)
  121.         (islamic-date (calendar-islamic-from-absolute
  122.                        (calendar-absolute-from-gregorian
  123.                         (or date (calendar-current-date))))))
  124.     (if (< (extract-calendar-year islamic-date) 1)
  125.         ""
  126.       (calendar-date-string islamic-date nil t))))
  127.  
  128. (defun calendar-print-islamic-date ()
  129.   "Show the Islamic calendar equivalent of the date under the cursor."
  130.   (interactive)
  131.   (let ((i (calendar-islamic-date-string (calendar-cursor-to-date t))))
  132.     (if (string-equal i "")
  133.         (message "Date is pre-Islamic")
  134.       (message "Islamic date (until sunset): %s" i))))
  135.  
  136. (defun calendar-goto-islamic-date (date &optional noecho)
  137.   "Move cursor to Islamic DATE; echo Islamic date unless NOECHO is t."
  138.   (interactive
  139.    (let* ((today (calendar-current-date))
  140.           (year (calendar-read
  141.                  "Islamic calendar year (>0): "
  142.                  '(lambda (x) (> x 0))
  143.                  (int-to-string
  144.                   (extract-calendar-year
  145.                    (calendar-islamic-from-absolute
  146.                     (calendar-absolute-from-gregorian today))))))
  147.           (month-array calendar-islamic-month-name-array)
  148.           (completion-ignore-case t)
  149.           (month (cdr (assoc
  150.                        (capitalize
  151.                         (completing-read
  152.                          "Islamic calendar month name: "
  153.                          (mapcar 'list (append month-array nil))
  154.                          nil t))
  155.                        (calendar-make-alist month-array 1 'capitalize))))
  156.           (last (islamic-calendar-last-day-of-month month year))
  157.           (day (calendar-read
  158.                 (format "Islamic calendar day (1-%d): " last)
  159.                 '(lambda (x) (and (< 0 x) (<= x last))))))
  160.      (list (list month day year))))
  161.   (calendar-goto-date (calendar-gregorian-from-absolute
  162.                        (calendar-absolute-from-islamic date)))
  163.   (or noecho (calendar-print-islamic-date)))
  164.  
  165. (defun diary-islamic-date ()
  166.   "Islamic calendar equivalent of date diary entry."
  167.   (let ((i (calendar-islamic-date-string (calendar-cursor-to-date t))))
  168.     (if (string-equal i "")
  169.         "Date is pre-Islamic"
  170.       (format "Islamic date (until sunset): %s" i))))
  171.  
  172. (defun holiday-islamic (month day string)
  173.   "Holiday on MONTH, DAY (Islamic) called STRING.
  174. If MONTH, DAY (Islamic) is visible, the value returned is corresponding
  175. Gregorian date in the form of the list (((month day year) STRING)).  Returns
  176. nil if it is not visible in the current calendar window."
  177.   (let* ((islamic-date (calendar-islamic-from-absolute
  178.                         (calendar-absolute-from-gregorian
  179.                          (list displayed-month 15 displayed-year))))
  180.          (m (extract-calendar-month islamic-date))
  181.          (y (extract-calendar-year islamic-date))
  182.         (date))
  183.     (if (< m 1)
  184.         n